<template>
	<uni-indexed-list :options="list"  @click="bindClick" />
</template>


<script>
	import {queryDs} from '@/api/dataset.js'
	export default {
		components: {},
		data() {
			return {
				list: []
			}
		},
		onLoad() {
			queryDs({code:'sys_user_constact'}).then(response=>{
				let dataMap = {};
				for(let i=0;i<response.data.length;i++){
					let name = response.data[i].name + '\t' +  response.data[i].phonenumber;
					let letter = name.substr(0,1);
					if(dataMap[letter]){
						dataMap[letter].push(name);
					}else{
						dataMap[letter] = [name];
					}
				}
				let dataList = [];
				for(let l in dataMap){
					dataList.push({
						letter: l,
						data: dataMap[l]
					});
				}
				this.list = dataList;
			})
		},
		methods: {
			bindClick(e) {				
				let np = e.item.name.split('\t');
				console.log(np.length)
				if(np.length < 2){
					uni.showToast({
						title: np[0] + '没有登记手机号',
						duration: 2000
					});
					return;
				}
				let actionList = [np[1]];
				
				// #ifdef APP-PLUS
				actionList.push('拨号');
				// #endif
				
				uni.showActionSheet({
					itemList: actionList,
					success: function (res) {
						if(res.tapIndex == 0){
							//复制
							uni.setClipboardData({
								data: np[1],
								success: function () {
									console.log('copy phone success');
								}
							});
						}else{
							//拨号
							uni.makePhoneCall({
								phoneNumber: np[1] //仅为示例
							});
						}
					},
					fail: function (res) {
						console.log(res.errMsg);
					}
				});
			}
		}
	}
</script>
<style>
</style>